﻿Shader "Custom/Green" 
{
	Properties 
	{
		_MainTex ("Base(RGB)", 2D) = "white"{}
		_RimColor ("Rim Color", Color) = (1, 1, 1, 1)  
        _RimPower ("Rim Width", Float) = 0.9  
		_RimIntensity("Rim Intensity", Range(0.0, 100)) = 3  
	}
	SubShader 
	{
		Tags { "RenderType"="Opaque" }
		Pass
		{
			Tags{"LightMode" = "ForwardBase"}

			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag 
			#include "UnityCG.cginc"
			#include "Lighting.cginc"
			#include "AutoLight.cginc"

			float4 _RimColor;
			float _RimPower;
			sampler2D _MainTex;
			float4 _MainTex_ST;
			float _RimIntensity;

			struct a2v
			{
				float4 vertex : POSITION;
				float3 normal : NORMAL;
				float4 texcoord : TEXCOORD0;
			};

			struct v2f
			{
				float4 pos : SV_POSITION;
				float3 worldNormal : TEXCOORD0;
				float3 worldPos : TEXCOORD1;
				float2 uv : TEXCOORD2;
			};

			v2f vert(a2v v)
			{
				v2f o;
				o.pos = UnityObjectToClipPos(v.vertex);
				o.worldNormal = UnityObjectToWorldNormal(v.vertex);
				o.worldPos = mul(unity_ObjectToWorld, v.vertex);
				o.uv = v.texcoord * _MainTex_ST.xy + _MainTex_ST.zw;
				return o;
			}

			fixed4 frag(v2f i) : COLOR
			{
				fixed3 worldNormal = normalize(i.worldNormal);
				fixed3 worldViewDir = normalize(UnityWorldSpaceViewDir(i.worldPos));
				fixed3 worldLightDir = normalize(UnityWorldSpaceLightDir(i.worldPos));
				fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
				fixed3 texColor = tex2D(_MainTex, i.uv).rgb;
				fixed3 diffuse = _LightColor0.rgb * texColor * max(0, dot(worldNormal, worldLightDir));
				UNITY_LIGHT_ATTENUATION(atten, i, i.worldPos);
				half Rim = 1.0 - max(0, dot(worldNormal, worldViewDir));  
				float3 emissive = _RimColor.rgb * pow(Rim, _RimPower) * _RimIntensity;  
				float3 finalColor = diffuse + emissive;  
				return fixed4(finalColor, 1.0);
			}
			ENDCG
		}
	}
	FallBack "Diffuse"
}
